//
//  LatestViewController.m
//  project_delicious
//
//  Created by 章历杰 on 15/3/24.
//  Copyright (c) 2015年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "LatestViewController.h"
#import "AFNetworking.h"
#import "Details.h"
#import "RecommCell.h"
#import "LatDetailsViewController.h"
#import "MJRefresh.h"
#import "MBProgressHUD.h"
@interface LatestViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) NSMutableArray *array;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) MBProgressHUD *HUD;
@end

@implementation LatestViewController
- (void)dealloc
{
    [_array release];
    [_tableView release];
    [_HUD release];
    [super dealloc];
}
//解析数据
- (void)startAnalyze:(NSString *)str
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *md = [responseObject objectForKey:@"data"];
        NSMutableArray *arr = [md objectForKey:@"app_index_latest"];
        //NSLog(@"%ld", self.array.count);
        for (NSMutableDictionary *dic in arr) {
            Details *details = [[Details alloc] initWithDictionary:dic];
            [self.array addObject:details];
            [details release];
        }
        //NSLog(@"%ld", self.array.count);
        self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.frame.size.width, self.view.frame.size.height) style:UITableViewStylePlain];
        self.tableView.dataSource = self;
        self.tableView.delegate = self;
        //self.tableView.showsVerticalScrollIndicator = NO;
        self.tableView.backgroundColor = [UIColor clearColor];
        self.tableView.separatorColor = [UIColor clearColor];
        [self.view addSubview:self.tableView];
        [_tableView release];
        //下拉刷新
        [self.tableView addHeaderWithTarget:self action:@selector(headerReference)];
        
        
        //上拉加载
        [self.tableView addFooterWithTarget:self action:@selector(footerReference)];
        
        //返回主页
        UIImageView *backMain = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-15, 30, 30, 30)];
        backMain.image = [UIImage imageNamed:@"backToRe2.png"];
        backMain.layer.masksToBounds = YES;
        backMain.userInteractionEnabled = YES;
        UITapGestureRecognizer *tapMain = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backMain)];
        [backMain addGestureRecognizer:tapMain];
        [tapMain release];
        [self.view addSubview:backMain];
        [backMain release];
        //自定义的返回View
        UIView *back = [[UIView alloc] initWithFrame:CGRectMake(10, 30, 30, 30)];
        back.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backToRecomm.png"]];
        back.layer.masksToBounds = YES;
        back.layer.cornerRadius = 15;
        back.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(back)];
        [back addGestureRecognizer:tap];
        [tap release];
        [self.view addSubview:back];
        [back release];

        
        [_HUD hide:YES];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"错误");
    }];

}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"最新";
    
    self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"IMG_1345.JPG"]];
    //隐藏navigation
    self.navigationController.navigationBarHidden = YES;
    //navigation左边返回
//    self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"backToRecomm.png"] style:UIBarButtonItemStylePlain target:self action:@selector(back)] autorelease];
//    self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;

    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.array = [NSMutableArray array];
    
    NSString *str = @"http://yinweiapi.paidui.com:81/jms/fetchData.do?procedureName=app_index_latest&parameters=%7B%22_end%22%3A%2215%22%2C%22_member_id%22%3A%220%22%2C%22_start%22%3A%220%22%7D";
    
    self.HUD = self.HUD = [[MBProgressHUD alloc] initWithView:self.view];
    _HUD.labelText = @"Loading";
    //_HUD.color = [UIColor clearColor];
    _HUD.mode = MBProgressHUDModeIndeterminate;
    [self.view addSubview:_HUD];
    [_HUD release];
    [_HUD show:YES];

    
    [self startAnalyze:str];
    
    //返回主页
    UIImageView *backMain = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width/2-15, 30, 30, 30)];
    backMain.image = [UIImage imageNamed:@"backToRe2.png"];
    backMain.layer.masksToBounds = YES;
    backMain.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapMain = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(backMain)];
    [backMain addGestureRecognizer:tapMain];
    [tapMain release];
    [self.view addSubview:backMain];
    [backMain release];
    //自定义的返回View
    UIView *back = [[UIView alloc] initWithFrame:CGRectMake(10, 30, 30, 30)];
    back.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"backToRecomm.png"]];
    back.layer.masksToBounds = YES;
    back.layer.cornerRadius = 15;
    back.userInteractionEnabled = YES;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(back)];
    [back addGestureRecognizer:tap];
    [tap release];
    [self.view addSubview:back];
    [back release];
    
    
}
//下拉刷新
- (void)headerReference
{
    
    NSString *str = @"http://yinweiapi.paidui.com:81/jms/fetchData.do?procedureName=app_index_latest&parameters=%7B%22_end%22%3A%2215%22%2C%22_member_id%22%3A%220%22%2C%22_start%22%3A%220%22%7D";
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *md = [responseObject objectForKey:@"data"];
        self.array = [NSMutableArray array];
        NSMutableArray *arr = [md objectForKey:@"app_index_latest"];
        //NSLog(@"%ld", self.array.count);
        for (NSMutableDictionary *dic in arr) {
            Details *details = [[Details alloc] initWithDictionary:dic];
            [self.array addObject:details];
            [details release];
        }
        
        [self.tableView headerEndRefreshing];
        [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"错误");
    }];
    
    
}

//上拉加载
- (void)footerReference
{
    
     //NSString *str = @"http://yinweiapi.paidui.com:81/jms/fetchData.do?procedureName=app_index_latest&parameters=%7B%22_end%22%3A%2215%22%2C%22_member_id%22%3A%220%22%2C%22_start%22%3A%220%22%7D";
    
    NSString *str1 = @"http://yinweiapi.paidui.com:81/jms/fetchData.do?procedureName=app_index_latest&parameters=%7B%22_end%22%3A%2215%22%2C%22_member_id%22%3A%220%22%2C%22_start%22%3A%22";
    static NSInteger a = 0;
    a = a + 10;
    NSString *str2 = [NSString stringWithFormat:@"%ld", (long)a];
    NSString *str3 = @"%22%7D";
    NSString *str = [NSString stringWithFormat:@"%@%@%@", str1, str2, str3];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSMutableDictionary *md = [responseObject objectForKey:@"data"];
        NSMutableArray *arr = [md objectForKey:@"app_index_latest"];
        for (NSMutableDictionary *dic in arr) {
            Details *details = [[Details alloc] initWithDictionary:dic];
            [self.array addObject:details];
            [details release];
        }
        
        [self.tableView footerEndRefreshing];
        [self.tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"错误");
    }];

    
}

//返回主页
- (void)backMain
{
    [self.navigationController popToRootViewControllerAnimated:NO];
}

//返回
- (void)back
{
    [self.navigationController popViewControllerAnimated:NO];
}
//cell数量
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.array.count;
}
//cell样式
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *str = @"reuse";
    RecommCell *cell = [tableView dequeueReusableCellWithIdentifier:str];
    if (cell == nil) {
        cell = [[[RecommCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:str] autorelease];
    }
    cell.backgroundColor = [UIColor clearColor];
    //崩
    cell.details = [self.array objectAtIndex:indexPath.item];
    return cell;
}
//cell高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 300;
}
//选中cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
   
    LatDetailsViewController *lat = [[LatDetailsViewController alloc] init];
    lat.num = [[self.array objectAtIndex:indexPath.item] member_share_id];
    //传count
//    RecommCell *cell2 = [[RecommCell alloc] init];
//    [cell2 setBlock:^(NSString *str) {
//        lat.favoCount = str;
//    }];
//    [cell2 release];
    
    lat.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:lat animated:NO];
    //lat.hidesBottomBarWhenPushed = NO;
    [lat release];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
